/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.service;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.recharge.entity.Recharge;
import com.boxin.ims.modules.recharge.entity.RechargeLog;
import com.boxin.ims.modules.recharge.dao.RechargeLogDao;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;

/**
 * 会员充值配置日志Service
 * @author Jakemanse
 * @version 2014-03-03
 */
@Component
@Transactional(readOnly = true)
public class RechargeLogService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(RechargeLogService.class);
	
	@Autowired
	private RechargeLogDao rechargeLogDao;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	@Autowired
	private RechargeService rechargeService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	public RechargeLog get(Long id) {
		return rechargeLogDao.findOne(id);
	}
	
	public Page<RechargeLog> find(Page<RechargeLog> page, RechargeLog rechargeLog) {
		DetachedCriteria dc = rechargeLogDao.createDetachedCriteria();
		if (rechargeLog.getUser()!=null && rechargeLog.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", rechargeLog.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(rechargeLog.getName())){
			dc.add(Restrictions.like("name", "%"+rechargeLog.getName()+"%"));
		}
		if(StringUtils.isNotEmpty(rechargeLog.getType())){
			System.out.println(!rechargeLog.getType().equals("3"));
			if(!rechargeLog.getType().equals("3")){
				dc.add(Restrictions.eq("type", rechargeLog.getType()));
			}
		}
		if(rechargeLog.getMember()!=null&&rechargeLog.getMember().getId()!=null){
				dc.add(Restrictions.eq("member.id", rechargeLog.getMember().getId()));
		}
		if(StringUtils.isNotEmpty(rechargeLog.getDay())){
			dc.add(Restrictions.eq("day", rechargeLog.getDay()));
		}
		dc.addOrder(Order.desc("id"));
		return rechargeLogDao.find(page, dc);
	}
	
	
	public List<RechargeLog> findByType( RechargeLog rechargeLog) {
		DetachedCriteria dc = rechargeLogDao.createDetachedCriteria();
		if (rechargeLog.getUser()!=null && rechargeLog.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", rechargeLog.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(rechargeLog.getName())){
			dc.add(Restrictions.like("name", "%"+rechargeLog.getName()+"%"));
		}
		if(StringUtils.isNotEmpty(rechargeLog.getType())){
				dc.add(Restrictions.eq("type", "2"));
		}
		if(rechargeLog.getMember()!=null&&rechargeLog.getMember().getId()!=null){
				dc.add(Restrictions.eq("member.id", rechargeLog.getMember().getId()));
		}
		if(StringUtils.isNotEmpty(rechargeLog.getDay())){
			dc.add(Restrictions.eq("day", rechargeLog.getDay()));
		}
		dc.add(Restrictions.eq("status", "0"));
		dc.addOrder(Order.desc("id"));
		return rechargeLogDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(RechargeLog rechargeLog) {
		
		if("2".equals(rechargeLog.getType())){
			
			int width = QRCodeUtils.WIDTH;
			int height =QRCodeUtils.HEIGHT;
			//生成二维码
			QRCode qrcode = new QRCode();
			String content = QRCodeUtils.generatePayMoneySN(rechargeLog.getUser().getId());
			qrcode.setContent(content);
			String path = Constants.getWechatResourceSavePath();
			path += DateUtils.getTodayString()+File.separator+content;
			FileUtils.createDirectory(path);
			QRCodeUtils.generateQRCodePNG(content, path, width,height);
			qrcode.setFileName(content+".png");
			qrcode.setPngPath(path+".png");
			
			qrcode.setUser(rechargeLog.getUser());
			qrcode.setMember(rechargeLog.getMember());
			qrcode.setType(QRCode.TYPE_PAY_MONEY);
			qrcode.setName("支付");
			
			qrCodeService.save(qrcode);	//保存二维码		
			
			rechargeLog.setQrCode(qrcode);
		}
		
		
		rechargeLog.setDay(DateUtils.getTodayString());
		rechargeLog.setMonth(com.thinkgem.jeesite.common.utils.DateUtils.getMonth());
		rechargeLog.setYear(DateUtils.getTodayString().substring(0,4));
		rechargeLogDao.save(rechargeLog);
	}
	
	@Transactional(readOnly = false)
	public void goBackMoney(RechargeLog rechargeLog,WechatMember member) {
		Recharge recharge=  rechargeService.getRechargeByMemberId(member.getId());
		
		rechargeLog.setStatus("2");
		rechargeLog.setRemarks("放弃前："+member.getMoney()+"---"+"放弃后："+(member.getMoney()+rechargeLog.getMoney()));
		rechargeLogDao.save(rechargeLog);
		
		recharge.setMoney(recharge.getMoney()+rechargeLog.getMoney());
		rechargeService.save(recharge);
		
		member.setMoney(member.getMoney()+rechargeLog.getMoney());
		wechatMemberService.save(member);
		
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		rechargeLogDao.deleteById(id);
	}
	
}
